<?php
/***********************************************************
	Filename: app/admin/control/cate.php
	Note	: 分类管理
	Version : 3.0
	Author  : qinggan
	Update  : 2009-12-18
***********************************************************/
class cate_c extends Control
{
	var $module_sign = "cate";
	function __construct()
	{
		parent::Control();
		$this->load_model("cate");//读取模块列表
	}

	function cate_c()
	{
		$this->__construct();
	}

	function index_f()
	{
		sys_popedom($this->module_sign.":list","tpl");
		$this->cate_m->langid($_SESSION["sys_lang_id"]);
		$this->cate_m->get_all();
		$this->cate_m->format_list(0,0);
		$catelist = $this->cate_m->flist();
		if(!is_array($catelist)) $catelist = array();
		foreach($catelist AS $key=>$value)
		{
			$value["space"] = "";
			for($i=0;$i<$value["level"];$i++)
			{
				$value["space"] .= "　　";
			}
			$catelist[$key] = $value;
		}
		$this->tpl->assign("catelist",$catelist);
		$this->tpl->display("cate/list.html");
	}

	function chk_f()
	{
		$sign = $this->trans_lib->safe("sign");
		if(!$sign)
		{
			exit("error: 标识串为空");
		}
		//检测标识串是否符合要求
		if(!ereg("[a-z][a-z0-9\_]+",$sign))
		{
			exit("error: 标识串仅限小写英文字母，数字及下划线，且第一位必须是字母");
		}
		//检测唯一性
		$rs = $this->cate_m->chksign($sign);
		if($rs)
		{
			exit("error: 标识串已被使用，请返回修改");
		}
		else
		{
			exit("ok");
		}
	}

	function set_f()
	{
		$order_list["post_date:desc"] = "最新发布排前";
		$order_list["post_date:asc"] = "最新发布排后";
		$order_list["modify_date:desc"] = "最新修改排前";
		$order_list["modify_date:asc"] = "最新修改排后";
		$order_list["replydate:desc"] = "最新回复排前";
		$order_list["replydate:asc"] = "最新回复排后";
		$order_list["hits:desc"] = "热门主题排前";
		$order_list["hits:asc"] = "冷门主题排前";
		$this->tpl->assign("order_list",$order_list);
		$id = $this->trans_lib->int("id");
		if($id)
		{
			sys_popedom($this->module_sign.":modify","tpl");
			$rs = $this->cate_m->get_one($id);
			$this->tpl->assign("rs",$rs);
		}
		else
		{
			sys_popedom($this->module_sign.":add","tpl");
			$this->cate_m->langid($_SESSION["sys_lang_id"]);
			$this->cate_m->get_all();
			$this->cate_m->format_list(0,0);
			$catelist = $this->cate_m->flist();
			foreach($catelist AS $key=>$value)
			{
				$value["space"] = "";
				for($i=0;$i<$value["level"];$i++)
				{
					$value["space"] .= "　　";
				}
				$catelist[$key] = $value;
			}
			$module_list = $this->cate_m->module_list();//取得模块列表
			$cate_html = "<select name='cate_id' id='cate_id'>";
			$cate_html.= "<option value=''>请选择…</option>";
			$cate_html.= "<optgroup label='设置根分类'>";
			foreach($module_list AS $key=>$value)
			{
				$cate_html.= "<option value='module:".$value["id"]."'>".$value["title"]."</option>";
			}
			$cate_html.= "</optgroup>";
			$cate_html.= "<optgroup label='选择子分类'>";
			foreach($catelist AS $key=>$value)
			{
				$cate_html.= "<option value='".$value["id"]."'>【".$value["title"]."】".$value["space"].$value["cate_name"]."</option>";
			}
			$cate_html.= "</optgroup>";
			$cate_html.= "</select>";
		}
		$this->tpl->assign("cate_html",$cate_html);
		//读取模板风格
		$this->load_model("tpl");
		$tpl_list = $this->tpl_m->get_list($_SESSION["sys_lang_id"],1);
		$this->tpl->assign("tpl_list",$tpl_list);
		//关联图片类型
		$this->load_model("gdtype");
		$gdlist = $this->gdtype_m->get_all();
		$this->tpl->assign("gdlist",$gdlist);
		$this->tpl->display("cate/set.html");
	}

	//存储分类信息
	function setok_f()
	{
		$id = $this->trans_lib->int("id");
		$array = array();
		$array["cate_name"] = $this->trans_lib->safe("cate_name");
		$array["tpl_id"] = $this->trans_lib->int("tpl_id");
		if($id)
		{
			sys_popedom($this->module_sign.":modify","tpl");
		}
		else
		{
			sys_popedom($this->module_sign.":add","tpl");
			$array["identifier"] = $this->trans_lib->safe("identifier");
			$array["langid"] = $_SESSION["sys_lang_id"];
			$parentid = $this->trans_lib->safe("cate_id");
			if(strlen($parentid)>6 && substr($parentid,0,6) == "module")
			{
				$array["module_id"] = substr($parentid,7);
				$array["parentid"] = 0;
			}
			else
			{
				$parentid = intval($parentid);
				$rs = $this->cate_m->get_one($parentid);
				$array["module_id"] = $rs["module_id"];
				$array["parentid"] = $parentid;
			}
		}
		$array["tpl_id"] = $this->trans_lib->int("tpl_id");//模板目录
		$array["tpl_index"] = $this->trans_lib->safe("tpl_index");
		$array["tpl_list"] = $this->trans_lib->safe("tpl_list");
		$array["tpl_file"] = $this->trans_lib->safe("tpl_file");
		$array["if_index"] = isset($_POST["if_index"]) ? 1 : 0;
		$array["status"] = isset($_POST["status"]) ? 1 : 0;
		$array["taxis"] = $this->trans_lib->int("taxis");
		$array["if_hidden"] = $this->trans_lib->int("if_hidden");
		$array["keywords"] = $this->trans_lib->safe("keywords");
		$array["description"] = $this->trans_lib->safe("description");
		$array["note"] = $this->trans_lib->safe("note");
		$array["ifguest"] = $this->trans_lib->int("ifguest");
		$array["chk_guest"] = isset($_POST["chk_guest"]) ? 1 : 0;
		$array["ifuser"] = $this->trans_lib->int("ifuser");
		$array["chk_user"] = isset($_POST["chk_user"]) ? 1 : 0;
		$array["ifreply"] = $this->trans_lib->int("ifreply");
		$array["chk_reply"] = isset($_POST["chk_reply"]) ? 1 : 0;
		$array["if_hits"] = $this->trans_lib->int("if_hits");
		$array["inpic"] = $this->trans_lib->safe("inpic");//读取默认图片
		$array["psize"] = $this->trans_lib->int("psize");
		if($array["psize"]<1)
		{
			$array["psize"] = 30;
		}
		$array["target"] = $this->trans_lib->int("target");
		$array["linkurl"] = $this->trans_lib->safe("linkurl");
		$array["ordertype"] = $this->trans_lib->safe("ordertype");
		//存储分类信息
		$this->cate_m->save($array,$id);
		error("分类信息添加/存储成功",site_url("cate"));
	}

	function ajax_status_f()
	{
		$id = $this->trans_lib->int("id");
		if(!$id)
		{
			exit("error:没有指定ID");
		}
		sys_popedom($this->module_sign.":check","ajax");
		$rs = $this->cate_m->get_one($id);
		$status = $rs["status"] ? 0 : 1;
		$this->cate_m->set_status($id,$status);
		exit("ok");
	}

	function ajax_del_f()
	{
		$id = $this->trans_lib->int("id");
		if(!$id)
		{
			exit("error:没有指定ID");
		}
		sys_popedom($this->module_sign.":delete","ajax");
		$rs = $this->cate_m->chk_son($id);
		if($rs)
		{
			exit("error: 对不起，您要删除的分类带有子分类，不允许删除");
		}
		//检测是否有内容
		unset($rs);
		$rs = $this->cate_m->chk_msg($id);
		if($rs)
		{
			exit("error: 对不起，您要删除的分类已经有内容了，不允许删除");
		}
		$this->cate_m->del($id);
		exit("ok");
	}

}
?>